package com.ziq.zcamp.place

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.blankj.utilcode.util.SpanUtils
import com.donkingliang.groupedadapter.holder.BaseViewHolder
import com.ziq.base.extension.BindingViewHolder
import com.ziq.base.extension.click
import com.ziq.base.extension.createBindingViewHolder
import com.ziq.base.extension.dp
import com.ziq.zcamp.R
import com.ziq.zcamp.activities.ActivitiesDetailsActivity
import com.ziq.zcamp.bean.activities.ActivitiesInfo
import com.ziq.zcamp.bean.place.DetailGroup
import com.ziq.zcamp.databinding.ItemPlaceDetailActBinding
import com.ziq.zcamp.databinding.ItemPlaceDetailActivitiesBannerBinding
import com.ziq.zcamp.util.ImageUtil
import com.ziq.zcamp.util.TimeUtil
import com.ziq.zcamp.view.recyclerview.AlignLeftPagerSnapHelper
import java.text.DecimalFormat

class PlaceDetailActGroup(val context : Context, val adapter: PlaceDetailAdapter) : DetailGroup() {
    override fun getHeaderViewType(groupPosition: Int): Int {
        return PlaceDetailAdapter.TYPE_HEADER_ACT
    }

    override fun getChildViewType(groupPosition: Int, childPosition: Int): Int {
        return PlaceDetailAdapter.TYPE_CHILD_ACT
    }

    override fun getFooterViewType(groupPosition: Int): Int {
        return 0
    }

    override fun getChildrenCount(): Int {
        return 1
    }

    override fun hasHeader(groupPosition: Int): Boolean {
        return true
    }

    override fun hasFooter(groupPosition: Int): Boolean {
        return false
    }

    var activities : List<ActivitiesInfo>? = null

    override fun onBindHeaderViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int
    ) {
    }

    override fun onBindChildViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ) {

        val layoutParams = holder?.itemView?.layoutParams
        if (layoutParams != null && layoutParams is StaggeredGridLayoutManager.LayoutParams) {
            layoutParams.isFullSpan = true
        }

        holder?.let {
            val binding = ItemPlaceDetailActBinding.bind(holder.itemView)
            if(adapter.hasBookingCard()){
                binding.container.setBackgroundResource(R.drawable.bg_white_0_0_0_0)
            } else {
                binding.container.setBackgroundResource(R.drawable.bg_white_0_0_5_5)
            }

            binding.rvAct.adapter = CommonAdapter(context, activities?: mutableListOf())
            binding.rvAct.layoutManager = LinearLayoutManager(binding.rvAct.context, RecyclerView.HORIZONTAL, false)
            binding.rvAct.onFlingListener = null
            AlignLeftPagerSnapHelper().attachToRecyclerView(binding.rvAct)
        }

    }

    override fun onBindFooterViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int
    ) {
    }


    private class CommonAdapter(val context : Context, val dataList : List<ActivitiesInfo>)
        : RecyclerView.Adapter<BindingViewHolder<ItemPlaceDetailActivitiesBannerBinding>>(){

        init {
            setHasStableIds(true)
        }

        override fun getItemCount(): Int {
            return dataList.size
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): BindingViewHolder<ItemPlaceDetailActivitiesBannerBinding> {
            return createBindingViewHolder(parent)
        }

        override fun onBindViewHolder(
            holder: BindingViewHolder<ItemPlaceDetailActivitiesBannerBinding>,
            position: Int
        ) {
            val item = dataList[position]
            val binding = ItemPlaceDetailActivitiesBannerBinding.bind(holder.itemView)
            val imageInfo = item.images?.let {
                if (it.isNotEmpty()){
                    it[0]
                }else{
                    null
                }
            }
            ImageUtil.updateImageFromNetwork(binding.ivActivitiesListPoster,imageInfo?.url, imageInfo?.width?:0,imageInfo?.height?:0)

            binding.tvActivitiesListName.text = item.title
            var timeStr = ""
            timeStr = if (item.start_time == null && item.end_time == null){
                "--:--"
            }else if (item.start_time != null && item.end_time == null){
                TimeUtil.yyyy_MM_dd_SPOT_FORMATTER_CN.format((item.start_time!!)*1000)
            }else{
                if (TimeUtil.isSameDay(item.start_time!!*1000,item.end_time!!*1000)){
                    TimeUtil.yyyy_MM_dd_SPOT_FORMATTER_CN.format((item.start_time!!)*1000)
                }else{
                    if (TimeUtil.isSameYear(item.start_time!!*1000,item.end_time!!*1000)){
                        "${TimeUtil.yyyy_MM_dd_SPOT_FORMATTER_CN.format((item.start_time!!)*1000)}-${TimeUtil.MM_dd_SPOT_FORMATTER_CN.format((item.end_time!!)*1000)}"
                    }else{
                        "${TimeUtil.yyyy_MM_dd_SPOT_FORMATTER_CN.format((item.start_time!!)*1000)}-${TimeUtil.yyyy_MM_dd_SPOT_FORMATTER_CN.format((item.end_time!!)*1000)}"
                    }
                }
            }
            binding.tvActivitiesListTime.text= timeStr

            val show = if (item?.act_status == 3){
                View.VISIBLE
            }else{
                View.GONE
            }
            binding.ivActivitiesListFinishSign.visibility = show
            binding.tvActivitiesListFinishSign.visibility = show

            setPriceText(binding.tvActivitiesListPrice,item.free_type == 1, DecimalFormat("0.00").format((item?.price_range?.minOrNull()?:0) / 100f))
            binding.slActivitiesListPosterMask.isClickable = false
            binding.root.click {
                ActivitiesDetailsActivity.open(context,item.id?:0,item, ActivitiesDetailsActivity.SOURCE_PLACE_DETAIL)
            }

        }

        @SuppressLint("RestrictedApi")
        private fun setPriceText(text : TextView, isFree : Boolean, content : String) {
            if (isFree){
                SpanUtils.with(text)
                    .append("免费").setForegroundColor(Color.parseColor("#729D55")).setTypeface(Typeface.DEFAULT_BOLD).setFontSize(13.dp)
                    .create()
            }else{
                SpanUtils.with(text)
                    .append("￥").setForegroundColor(Color.parseColor("#AF8B6C")).setFontSize(12.dp)
                    .append(content).setForegroundColor(Color.parseColor("#AF8B6C")).setFontSize(22.dp)
                    .append(" 起").setForegroundColor(Color.parseColor("#AF8B6C")).setFontSize(9.dp)
                    .create()
            }
        }
    }

}